package cz.ctu.android.DriveCommunicator.activities;

import java.util.ArrayList;
import java.util.List;

import cz.ctu.android.DriveCommunicator.R;
import cz.ctu.android.DriveCommunicator.utils.DriverConstants;
import cz.ctu.android.DriveCommunicator.utils.HandlerMessage;
import cz.ctu.android.DriveCommunicator.utils.ImageMapper;
import cz.ctu.android.DriveCommunicator.utils.Zprava;
import cz.ctu.android.adapters.CustomListViewAdapter;
import cz.ctu.android.entity.RowItem;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class UnreadMessage extends Activity{
	
	RadioButton conversationRadio;
	RadioButton unreadButton;
	ListView listView;
	List<RowItem> rowItems;
	TextView text;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.un_read_messages);

		loadScreen();


	}
	public void loadScreen(){
		text = (TextView)findViewById(R.id.noConversationFoundText);

		listView = (ListView) findViewById(R.id.tomList);
		ListAdapter adList = listView.getAdapter();
		conversationRadio = (RadioButton)findViewById(R.id.conversationRadio);
		unreadButton = (RadioButton)findViewById(R.id.unreadMessage);
		conversationRadio.setOnClickListener(conversationClick);
			conversationRadio.setChecked(false);
			unreadButton.setChecked(true);
		HandlerMessage hl = new HandlerMessage();
		rowItems = new ArrayList<RowItem>();

		for (int i = 1; i < DriverConstants.numberOfUsers+1; i++) {
			ArrayList<Zprava> conversation =  hl.getConversetion(i);
			if(conversation != null && !conversation.isEmpty()){
				Zprava zprava = conversation.get(0);
				if(zprava.isPrectena()){
					continue;
				}
				RowItem item;
				String sender = zprava.getJmeno() + " "+zprava.getPrijmeni();
				if(zprava.getSenderID() == DriverConstants.myId){
					sender = sender + " - sended";
				}
				else if(!zprava.isPrectena()){
						sender = sender +" *";
					}
				item = new RowItem(ImageMapper.getImage(i),i,sender , zprava.getZprava());	
				rowItems.add(item);
			}
		}
		if(rowItems != null && !rowItems.isEmpty()){
			text.setVisibility(3);
			
			CustomListViewAdapter adapter = new CustomListViewAdapter(this,
					R.layout.list_item, rowItems);
			listView.setAdapter(adapter);
			listView.setOnItemClickListener(onItemClickListener);
		}
		else{
			text.setText("No conversation found");
			text.setVisibility(1);
			listView.setVisibility(3);
		}
	}
	public OnItemClickListener onItemClickListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			Toast toast = Toast.makeText(getApplicationContext(), "Item "
					+ (position + 1) + ": " + rowItems.get(position),
					Toast.LENGTH_SHORT);
			toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
			toast.show();
			
			Bundle bundle = new Bundle();
			CustomListViewAdapter cw = (CustomListViewAdapter)parent.getAdapter();
			RowItem row = cw.getItem(position);
			HandlerMessage hm = new HandlerMessage();
			hm.markAsRead(row.getId());
			bundle.putInt("idToLoad", row.getId());
			Intent newIntent = new Intent(UnreadMessage.this, MessageActivity.class);
			newIntent.putExtras(bundle);
	        startActivity(newIntent);
		}
	
	};
	
	
	
	private OnClickListener conversationClick = new OnClickListener() {
		
		public void onClick(View v) {
			Intent newIntent = new Intent(UnreadMessage.this, ConversationActivity.class);
	        startActivity(newIntent);
			
		}
	};
}
